/**
 * 关联产品分类
 * @author linfeng
 * @since 2019/08/19
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;

    form.render(null, "zmn-categ-form");

    let ajaxIndex;
    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        let url = "/tariffProduct/relation/batchAddRelation.action";
        //前台产品Id
        let param = {tariffProductId:json.tariffProductId};
        let selectProductIds = new Array();
        $("input:checkbox:checked").each((i,e)=>{selectProductIds.push($(e).val())});
        param.productIds = selectProductIds;

        if(selectProductIds.length == 0){
            layer.msg("关联数据为空,请选择");
            return false;
        }

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json",
            data: JSON.stringify(param),
            beforeSend: function () {
                ajaxIndex = layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            },
            success:function(json) {
                setTimeout(function () {
                    if(json.status == 200) {
                        layer.msg(json.message);
                        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index);
                    } else {
                        layer.msg(json.message, {"icon": 2});
                    }
                },1000);
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){}
        })

        layer.close(ajaxIndex);
        return false;
    });

    //点击取消
    $(document).on('click', '#cancel', function () {
        zmnLayUtil.closeThisTabs();
    });

    //对外暴露的接口
    exports('art_product_relation_tariff_edit', {});
});